package  
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.getTimer;
	import xxl.signals.base.Signal;
	import xxl.signals.base.Slot;
	import xxl.signals.core.ISlot;
	
	/**
	 * ...
	 * @author XXL
	 */
	public class Main4 extends Sprite 
	{
		private var txt:TextField = new TextField();
		
		public function Main4() 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			addChild(txt);
			txt.width = stage.stageWidth;
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.wordWrap = txt.multiline = true;
			
			//stage.addEventListener(MouseEvent.CLICK, cHandler);
			stage.addEventListener(MouseEvent.CLICK, cHandler2);
			
		}
		
		private function cHandler2(e:MouseEvent):void 
		{
			var slot:Slot = new Slot(function(...args):void { }, new Signal());
			var REPS:int = 1000000;
			var time:int;
			var i:int;
			var str:String = "";
			
			time = getTimer();
			for (i = 0; i < REPS; i++)
			{
				slot.execute([]);
				slot.execute([1]);
				slot.execute([1, 2]);
				slot.execute([1, 2, 3]);
				slot.execute([1, 2, 3, 4]);
				slot.execute([1, 2, 3, 4, 5]);
				slot.execute([1, 2, 3, 4, 5, 6]);
				slot.execute([1, 2, 3, 4, 5, 6, 7]);
				slot.execute([1, 2, 3, 4, 5, 6, 7, 8]);
				slot.execute([1, 2, 3, 4, 5, 6, 7, 8, 9]);
				slot.execute([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
				slot.execute([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
			}
			str += "execute:" + (getTimer() - time) + "\n";
			
			time = getTimer();
			for (i = 0; i < REPS; i++)
			{
				slot.executeTree([]);
				slot.executeTree([1]);
				slot.executeTree([1, 2]);
				slot.executeTree([1, 2, 3]);
				slot.executeTree([1, 2, 3, 4]);
				slot.executeTree([1, 2, 3, 4, 5]);
				slot.executeTree([1, 2, 3, 4, 5, 6]);
				slot.executeTree([1, 2, 3, 4, 5, 6, 7]);
				slot.executeTree([1, 2, 3, 4, 5, 6, 7, 8]);
				slot.executeTree([1, 2, 3, 4, 5, 6, 7, 8, 9]);
				slot.executeTree([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
				slot.executeTree([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
			}
			str += "executeA:" + (getTimer() - time) + "\n";
			
			txt.text = str;
		}
		
		private function cHandler(e:MouseEvent):void 
		{
			var str:String = "";
			
			var vec1:Vector.<Slot> = new Vector.<Slot>();
			var vec2:Vector.<ISlot> = new Vector.<ISlot>();
			var REPS:int = 100000;
			var LEN:int = 20;
			var i:int;
			var j:int;
			var time:int;
			for (i = 0; i < LEN; i++)
			{
				vec1[i] = new Slot(function():void { }, null);
				vec2[i] = new Slot(function():void { }, null);
			}
			
			time = getTimer();
			for (j = 0; j < REPS; j++)
			{
				for (i = 0; i < LEN; i++)
				{
					vec1[i].execute([]);
				}
			}
			str += "Slot:" + (getTimer() - time) + "\n";
			
			time = getTimer();
			for (j = 0; j < REPS; j++)
			{
				for (i = 0; i < LEN; i++)
				{
					vec2[i].execute([]);
				}
			}
			str += "ISlot:" + (getTimer() - time) + "\n";
			
			txt.text = str;
		}
		
	}

}